<template>
  <view class="page">
    <!-- 状态栏占位 -->
    <view :style="{ height: statusBarHeight + 'px' }"></view>

    <!-- 导航栏 -->
    <view class="nav-bar">
      <view class="nav-left" @tap="goBack">
        <text class="fa fa-chevron-left fa-gray"></text>
      </view>
      <text class="nav-title">病史采集的关键要点</text>
    </view>

    <!-- 内容区域 -->
    <scroll-view scroll-y class="content">
      <!-- 视频播放器 -->
      <view class="video-container">
        <view class="video-player" @tap="togglePlay">
          <!-- 视频封面/播放器 -->
          <view v-if="!isPlaying" class="video-cover">
            <view class="play-button">
              <text class="fa fa-play"></text>
            </view>
            <text class="cover-text">点击播放视频</text>
            <text class="video-duration">08:36</text>
          </view>

          <!-- 使用uniapp的video组件 -->
          <video
            v-else
            id="myVideo"
            class="video-element"
            :src="videoUrl"
            :controls="true"
            :show-center-play-btn="true"
            :enable-progress-gesture="true"
            @timeupdate="onTimeUpdate"
            @ended="onVideoEnd"
          ></video>
        </view>

        <!-- 视频控制栏（当未使用原生控件时） -->
        <view v-if="!isPlaying" class="video-controls">
          <view class="controls-wrapper">
            <view class="play-btn" @tap="togglePlay">
              <text class="fa fa-play"></text>
            </view>
            <view class="progress-area">
              <u-line-progress
                :percentage="videoProgress"
                activeColor="#ffffff"
                height="8"
              ></u-line-progress>
            </view>
            <text class="time-text">{{ currentTime }} / {{ totalTime }}</text>
          </view>
        </view>
      </view>

      <!-- 视频信息 -->
      <view class="video-info">
        <view class="info-card">
          <text class="video-title">病史采集的关键要点</text>
          <view class="video-meta">
            <text class="meta-item"
              ><text class="fa fa-play" style="margin-right: 8rpx"></text> 视频
            </text>
            <text class="meta-item"
              ><text class="fa fa-clock-o" style="margin-right: 8rpx"></text>
              08:36
            </text>
          </view>
          <text class="video-desc">
            本节课程详细介绍病史采集的标准流程，包括主诉、现病史、既往史等各个环节的询问技巧和注意事项，帮助医生提高临床诊断能力。
          </text>
        </view>
      </view>

      <!-- 学习进度 -->
      <view class="learning-progress">
        <view class="progress-card">
          <view class="progress-header">
            <text class="progress-title">学习进度</text>
            <text class="progress-percent"
              >{{ videoProgress.toFixed(0) }}%</text
            >
          </view>
          <view class="progress-bar-wrapper">
            <u-line-progress
              :percentage="videoProgress"
              activeColor="#3b82f6"
              height="10"
            ></u-line-progress>
          </view>
          <text class="progress-text">
            已学习 {{ Math.floor(currentSeconds / 60) }} 分钟，还需{{
              Math.floor((totalSeconds - currentSeconds) / 60)
            }}分钟完成
          </text>
        </view>
      </view>

      <view class="bottom-spacer"></view>
    </scroll-view>
  </view>
</template>

<script>
import uLineProgress from "@/uni_modules/uview-ui/components/u-line-progress/u-line-progress.vue";

export default {
  components: {
    uLineProgress,
  },
  data() {
    return {
      statusBarHeight: 0,
      isPlaying: false,
      videoProgress: 35, // 当前播放进度百分比
      currentSeconds: 182, // 3分02秒
      totalSeconds: 516, // 8分36秒
      currentTime: "03:02",
      totalTime: "08:36",
      videoUrl: "https://www.w3school.com.cn/example/html5/mov_bbb.mp4", // 示例视频URL
    };
  },
  onLoad() {
    this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight || 0;
  },
  methods: {
    goBack() {
      uni.navigateBack();
    },
    togglePlay() {
      this.isPlaying = !this.isPlaying;
      if (this.isPlaying) {
        // 开始播放
        this.$nextTick(() => {
          const videoContext = uni.createVideoContext("myVideo", this);
          videoContext.play();
        });
      }
    },
    onTimeUpdate(e) {
      const current = e.detail.currentTime;
      const duration = e.detail.duration;

      this.currentSeconds = Math.floor(current);
      this.totalSeconds = Math.floor(duration);

      // 更新进度
      this.videoProgress = (current / duration) * 100;

      // 格式化时间
      this.currentTime = this.formatTime(current);
      this.totalTime = this.formatTime(duration);
    },
    onVideoEnd() {
      this.isPlaying = false;
      this.videoProgress = 100;
      uni.showToast({
        title: "视频播放完成",
        icon: "success",
      });
    },
    formatTime(seconds) {
      const mins = Math.floor(seconds / 60);
      const secs = Math.floor(seconds % 60);
      return `${String(mins).padStart(2, "0")}:${String(secs).padStart(
        2,
        "0"
      )}`;
    },
    goToHome() {
      uni.navigateTo({
        url: "/pages/home",
      });
    },
    goToProfile() {
      uni.navigateTo({
        url: "/pages/profile",
      });
    },
  },
};
</script>

<style scoped>
.page {
  min-height: 100vh;
  background: linear-gradient(to bottom right, #f9fafb, #eff6ff);
  display: flex;
  flex-direction: column;
}

/* 导航栏 */
.nav-bar {
  height: 88rpx;
  background: #fff;
  border-bottom: 1rpx solid #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  padding: 0 32rpx;
}

.nav-left {
  position: absolute;
  left: 32rpx;
}

.nav-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #111827;
  text-align: center;
}

/* 内容区域 */
.content {
  flex: 1;
  padding-bottom: 120rpx;
}

/* 视频容器 */
.video-container {
  position: relative;
  background: #000;
}

.video-player {
  width: 100%;
  height: 0;
  padding-bottom: 56.25%; /* 16:9 宽高比 */
  position: relative;
}

.video-cover {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(to bottom right, #1f2937, #111827);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16rpx;
}

.play-button {
  width: 128rpx;
  height: 128rpx;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 32rpx;
}

.play-icon {
  font-size: 48rpx;
  color: #fff;
}

.cover-text {
  font-size: 28rpx;
  color: rgba(255, 255, 255, 0.9);
}

.video-duration {
  font-size: 24rpx;
  color: rgba(255, 255, 255, 0.7);
}

.video-element {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

/* 视频控制栏 */
.video-controls {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.6), transparent);
  padding: 32rpx;
}

.controls-wrapper {
  display: flex;
  align-items: center;
  gap: 24rpx;
  color: #fff;
}

.play-btn {
  width: 64rpx;
  height: 64rpx;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 28rpx;
}

.progress-area {
  flex: 1;
}

/* 视频信息 */
.video-info {
  font-size: 24rpx;
  color: #fff;
  flex-shrink: 0;
}

/* 视频信息 */
.video-info {
  padding: 32rpx;
}

.info-card {
  background: #fff;
  border-radius: 32rpx;
  padding: 32rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
  border: 1rpx solid #f3f4f6;
}

.video-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #111827;
  display: block;
  margin-bottom: 16rpx;
}

.video-meta {
  display: flex;
  gap: 24rpx;
  margin-bottom: 24rpx;
}

.meta-item {
  font-size: 24rpx;
  color: #6b7280;
}

.video-desc {
  font-size: 28rpx;
  color: #374151;
  line-height: 1.6;
  display: block;
}

/* 学习进度 */
.learning-progress {
  padding: 0 32rpx;
}

.progress-card {
  background: #fff;
  border-radius: 32rpx;
  padding: 32rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
  border: 1rpx solid #f3f4f6;
}

.progress-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24rpx;
}

.progress-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #111827;
}

.progress-percent {
  font-size: 28rpx;
  font-weight: 500;
  color: #2563eb;
}

.progress-bar-wrapper {
  margin-bottom: 24rpx;
}

.progress-text {
  width: 100%;
  font-size: 28rpx;
  color: #4b5563;
  display: block;
}

/* 底部留白 */
.bottom-spacer {
  height: 48rpx;
}

/* 底部导航 */
.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 120rpx;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(20rpx);
  border-top: 1rpx solid #f3f4f6;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  align-items: center;
  padding-bottom: env(safe-area-inset-bottom);
}

.tab-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8rpx;
  color: #6b7280;
}

.tab-item-active {
  color: #2563eb;
}

.tab-icon {
  font-size: 36rpx;
}

.tab-label {
  font-size: 20rpx;
}

.tab-item-active .tab-label {
  font-weight: 500;
}
</style>
